package com.vone.until.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

//数据库链接配置文件
public class MyJdbcConfig {
	//配置文件存放目录
		public static String CONFIG_URL="";
		//项目根目录
		public static String ROOT_URL="";
		
		static{
			String path = MyJdbcConfig.class.getProtectionDomain().getCodeSource().getLocation().getPath();
			File f=new File(path);
			f=f.getParentFile().getParentFile();
			ROOT_URL=f.getAbsolutePath();
			try {
				ROOT_URL=URLDecoder.decode(ROOT_URL, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			CONFIG_URL=ROOT_URL+"/config/";
			File file3=new File(CONFIG_URL);
			file3.mkdirs();
		}
		
		
	private static Properties myJdbcConfig = new Properties();// 用于存放从本地包读取的文件
	private static Properties myJdbcConfigSys = new Properties();// 用于存放从jar包读取的文件

	public static void init() {
		// 读取窗体字体调节尺寸配置文件路径
		String localeFile = "config/MyJdbcConfig.properties";
		try {
			// 读取本地窗体字体调节尺寸信息文件
			myJdbcConfig.load(new InputStreamReader(new FileInputStream(new File("./" + localeFile).getAbsolutePath()), "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
			// 没有读取到配置文件,从jar包读取,并且复制到目录
			try {
				myJdbcConfig.load(new InputStreamReader(MyJdbcConfig.class.getResourceAsStream("/" + localeFile), "UTF-8"));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			FileUtil.copyConfigFile(localeFile);
		} finally {
			try {
				myJdbcConfigSys.load(new InputStreamReader(MyJdbcConfig.class.getResourceAsStream("/" + localeFile), "UTF-8"));
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public static String getInfo(String key) {
		String value = myJdbcConfig.getProperty(key);
		if (value == null || "null".equalsIgnoreCase(value)) {
			value = myJdbcConfigSys.getProperty(key);
		}
		if (value == null || "null".equalsIgnoreCase(value)) {
			value = "";
		}
		return value;
	}
}